package cn.edu.scau.cmi.liweihang.spring.hibernate.dao;

import java.util.List;

import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.edu.scau.cmi.liweihang.spring.hibernate.dao.BaseHibernateDAO;
import cn.edu.scau.cmi.liweihang.spring.hibernate.domain.Student;
import cn.edu.scau.cmi.liweihang.spring.hibernate.domain.Teacher;


public class StudentDAO extends BaseHibernateDAO implements IStudentDAO{
	private static final Logger log = LoggerFactory.getLogger(StudentDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String NUMBER = "number";

	/**
	 * 添加一个学生数据
	 * @param transientInstance
	 */
	public void save(Student transientInstance) {
		log.debug("saving Student instance");
		try {
			getSession().beginTransaction();
			getSession().save(transientInstance);
			log.debug("save successful");
			getSession().getTransaction().commit();
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	/**
	 * 删除一条学生数据
	 * @param persistentInstance
	 */
	public Student delete(Student persistentInstance) {
		log.debug("deleting Student instance");
		try {
			getSession().beginTransaction();
			getSession().delete(persistentInstance);
			log.debug("delete successful");
			getSession().getTransaction().commit();
			return persistentInstance;
		} catch (RuntimeException re) {
			return null;
		}
	}
	
	/**
	 * 学生修改指导老师，学生选择指导老师
	 * @param updateId
	 * @param teacherId
	 * @return
	 */
    public Student updateTutor(Long studentId,Long teacherId) {
    	log.debug("updating Student instance");
		try {
			getSession().beginTransaction();
			Student student=(Student) getSession().get(Student.class, studentId);
			Teacher tutor=(Teacher) getSession().get(Teacher.class, teacherId);
			student.setTeacher(tutor);
			getSession().getTransaction().commit();
			return student;
		} catch (Exception e) {
			// TODO: handle exception
			log.error("update failed,",e);
			throw e;
		}
    }

	
	/**
	 * 修改学生名字
	 * @param name
	 * @param updateId
	 */
	public Student updateName(String name,Long updateId) {
		log.debug("updating Student instance");
		try {
			getSession().beginTransaction();
			Student student=(Student) getSession().get(Student.class, updateId);
			student.setName(name);
			getSession().getTransaction().commit();
			return student;
		} catch (Exception e) {
			// TODO: handle exception
			log.error("update failed,",e);
			throw e;
		}
	}
	
	public void update(Student instance) {
		log.debug("update a student instance");
		try {
			getSession().update(instance);
			log.debug("update successful");
		}catch(RuntimeException re) {
			log.error("update failed",re);
			throw re;
		}
	}
	
	/**
	 * 根据id查找学生
	 * @param id
	 * @return
	 */
	public Student findById(java.lang.Long id) {
		log.debug("getting Student instance with id: " + id);
		try {
			Student instance = (Student) getSession().get(Student.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/**
	 * 查找所有学生
	 * @return
	 */
	public List findAll() {
		log.debug("finding all Student instances");
		try {
			String queryString = "from Student";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

}